/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.mail.vaadin;

import com.liferay.mail.model.Account;
import com.liferay.mail.model.Folder;
import com.liferay.mail.vaadin.Controller.MessageModifiedListener;

import com.vaadin.ui.Accordion;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.BaseTheme;

import java.util.List;

/**
 * @author Henri Sara
 */
public class Folders extends CustomComponent
	implements FolderChangeListener, MessageModifiedListener {

	private VerticalLayout mainLayout;
	private VerticalLayout accountLayout;
	private Accordion mainPanel;

	private MessageList messageList;
	private FolderTree tree;
	private FolderContainer folderContainer;

	/**
	 * The constructor should first build the main layout, set the composition
	 * root and then do any custom initialization. The constructor will not be
	 * automatically regenerated by the visual editor.
	 */
	public Folders(MessageList messageList) {

		this.messageList = messageList;

		mainLayout = new VerticalLayout();
		mainLayout.setSizeFull();

		mainPanel = new Accordion();
		mainPanel.setSizeFull();
		mainPanel.setStyleName("borderless");

		accountLayout = new VerticalLayout();
		// accountLayout.setCaption(Lang.get("accounts"));

		mainLayout.addComponent(createComposeButton());
		mainLayout.addComponent(mainPanel);
		mainLayout.setExpandRatio(mainPanel, 1);

		mainPanel.addComponent(accountLayout);

		tree = new FolderTree(this, messageList);
		accountLayout.addComponent(tree);

		// top-level component properties
		setSizeFull();
		setCompositionRoot(mainLayout);

		Controller.get().addListener(this);
	}

	private Button createComposeButton() {

		Button compose = new NativeButton("Compose new message");
		compose.addListener(new Button.ClickListener() {

			public void buttonClick(ClickEvent event) {
				Controller.get().getApplication().switchToCompose(new Composer(), null);
			}
		});

		compose.setStyleName("compose");
		compose.setWidth("100%");

		return compose;
	}

	public void setAccounts(List<Account> accounts) {

		folderContainer = new FolderContainer(accounts);

		tree.setContainerDataSource(folderContainer);
	}

	public void setFolders(List<Folder> folders) {

		for (Folder folder : folders) {
			Button b = new Button(folder.getDisplayName());
			b.setStyleName(BaseTheme.BUTTON_LINK);
			b.setData(folder);
			b.addListener(new ClickListener() {

				public void buttonClick(ClickEvent event) {

					showFolder(((Folder) event.getButton().getData()));
				}
			});

		}
	}

	private void showFolder(Folder folder) {

		if (folder == null) {
			showFolder(null, null);
		}
		else {
			showFolder(folder.getAccountId(), folder);
		}
	}

	private void showFolder(Long accountId, Folder folder) {

		if (accountId != null && folder != null) {
			// Make sure the folder is expanded
			tree.expand(accountId, folder.getFolderId());
		}

		messageList.showMessages(accountId, folder);
	}

	public void showInbox(Account account) {

		tree.selectFolder(account.getAccountId(), account.getInboxFolderId());

	}

	public Account getCurrentAccount() {

		return folderContainer.getAccount(tree.getValue());
	}

	public void selectedFolderChanged(Folder folder) {

		showFolder(folder);
	}

	public void messageModified(long messageId) {

		tree.requestRepaint();
	}

	public void messagesAddedOrRemoved() {

		// Not interested in these events
	}

	public void refresh() {
		tree.refresh();
	}

}

interface FolderChangeListener {

	public void selectedFolderChanged(Folder folder);

}